package net.xdclass.controller;

import com.google.code.kaptcha.impl.DefaultKaptcha;
import lombok.extern.slf4j.Slf4j;
import net.xdclass.controller.Request.SendCodeRequest;
import net.xdclass.enums.BizCodeEnum;
import net.xdclass.enums.SendCodeEnum;
import net.xdclass.service.NotifyService;
import net.xdclass.util.CommonUtil;
import net.xdclass.util.JsonData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.scheduling.annotation.Async;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;

import javax.imageio.ImageIO;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.io.OutputStream;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
import java.util.Random;
import java.util.concurrent.TimeUnit;

@RestController
@Slf4j
@RequestMapping("/api/v1/notify")
public class NotifyController {
	@Autowired
	NotifyService notifyService;
    @Autowired
	DefaultKaptcha defaultKaptcha;

    @Autowired
	@Qualifier("redisTemplate")
	RedisTemplate redisTemplate;

    private   final static long KEY_EXPIRE_TIME = 1000 * 60 ;
	@GetMapping("send")
	@Async("threadPoolTaskExecutor")
	public JsonData send(){
	 	// notifyService.send();

		return  JsonData.buildSuccess();
	}

	@GetMapping("getCaptcha")
	public void getCaptcha(HttpServletRequest request, HttpServletResponse response){
		 String captchaText = defaultKaptcha.createText();
		 BufferedImage image = defaultKaptcha.createImage(captchaText);
		 log.info("验证码内容：{}",captchaText);
		redisTemplate.opsForValue().set(getCaptchaKey(request),captchaText,KEY_EXPIRE_TIME, TimeUnit.MILLISECONDS);
//		 try {OutputStream outputStream = response.getOutputStream();
////		     ImageIO.write(image,"jpg",outputStream);
////		     outputStream.flush();
////		     outputStream.close();
////		 }
	// jdk 1.7 新特性语法优化
	     try(OutputStream outputStream = response.getOutputStream();){
			 ImageIO.write(image,"jpg",outputStream);
			 outputStream.flush();
		 }
		 catch(IOException e){
		 	log.error("流报错：{}",e.getMessage());
		 }
	}

	public String getCaptchaKey(HttpServletRequest request){
		String ip = CommonUtil.getIpAddr(request);
		String userAgent = request.getHeader("User-agent");
		String key = "Account-service:Captcha:"+ CommonUtil.MD5(ip+userAgent);
		log.info("key的值是:{}",key);
		return key;
	}

	@PostMapping("/sendMessage")
	public JsonData sendMessage(@RequestBody SendCodeRequest sendCodeRequest,HttpServletRequest request){
		//	notifyService.send();
		String user_captcha = sendCodeRequest.getCaptcha();
		String key = getCaptchaKey(request);
		String cli_captcha = (String) redisTemplate.opsForValue().get(key);
		if(user_captcha != null && cli_captcha != null && user_captcha.equalsIgnoreCase(cli_captcha)){
			redisTemplate.delete(key);
			JsonData jsonData = notifyService.sendMessage(SendCodeEnum.USER_REGISTER,sendCodeRequest.getTo());
			return  jsonData;
		}else{
			return JsonData.buildResult(BizCodeEnum.CODE_CAPTCHA_ERROR);
		}
	}
}
